package com.example.administrator.yuejian2.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.administrator.yuejian2.R;
import com.example.administrator.yuejian2.activity.ShoppingCartActivity;
import com.example.administrator.yuejian2.activity.TrainDetailActivity;
import com.example.administrator.yuejian2.bean.ShoppingCartBean;
import com.example.administrator.yuejian2.bean.TrainOrderBean;

import java.util.List;

public class ShoppingCartAdapterH extends RecyclerView.Adapter{

    private Context context;
    private List<TrainOrderBean> shoppingCartBeanList;
    private ShoppingCartAdapterH.CheckInterface checkInterface;

    public ShoppingCartAdapterH(Context context)
    {
        this.context=context;
    }
    public void getShopdata(List<TrainOrderBean> shoppingCartBeanList)
    {
        this.shoppingCartBeanList=shoppingCartBeanList;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.shopping_item, parent, false);
       return new ShoppingCartViewHolderH(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        final ShoppingCartViewHolderH shoppingCartViewHolderH=(ShoppingCartViewHolderH)holder;
        final TrainOrderBean shoppingCartBean = shoppingCartBeanList.get(position);
        boolean choosed = shoppingCartBean.isChoosed();
        if (choosed){
            shoppingCartViewHolderH.ckOneChose.setChecked(true);//多选框
        }else{
            shoppingCartViewHolderH.ckOneChose.setChecked(false);//多选框
        }
        shoppingCartViewHolderH.tvCommodityName.setText(shoppingCartBean.getTrain_name());
        shoppingCartViewHolderH.tvCommodityPrice.setText(shoppingCartBean.getTrain_price()+"");
        shoppingCartViewHolderH.tvCommodityNum.setText(" X"+ ShoppingCartActivity.num+"");

        //单选框按钮
        shoppingCartViewHolderH.ckOneChose.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        shoppingCartBean.setChoosed(((CheckBox) v).isChecked());
                        checkInterface.checkGroup(position, ((CheckBox) v).isChecked());//向外暴露接口
                    }
                }
        );

    }

    @Override
    public int getItemCount() {
            if (shoppingCartBeanList==null)
            {
                return 0;
            }
            else
        return shoppingCartBeanList.size();
    }
    public void setCheckInterface(ShoppingCartAdapterH.CheckInterface checkInterface) {
        this.checkInterface = checkInterface;
    }



    class ShoppingCartViewHolderH extends RecyclerView.ViewHolder {

        public ImageView ivShowPic;
        public TextView tvCommodityName, tvCommodityAttr, tvCommodityPrice, tvCommodityNum, tvCommodityShowNum,ivSub, ivAdd;
        public CheckBox ckOneChose;
        public LinearLayout rlEdit;
        public ShoppingCartViewHolderH(View itemView) {
            super(itemView);
            ckOneChose = (CheckBox) itemView.findViewById(R.id.ck_chose);
            ivShowPic = (ImageView) itemView.findViewById(R.id.iv_show_pic);
            ivSub = (TextView) itemView.findViewById(R.id.iv_sub);
            ivAdd = (TextView) itemView.findViewById(R.id.iv_add);
            tvCommodityName = (TextView) itemView.findViewById(R.id.tv_commodity_name);
            tvCommodityAttr = (TextView) itemView.findViewById(R.id.tv_commodity_attr);
            tvCommodityPrice = (TextView) itemView.findViewById(R.id.tv_commodity_price);
            tvCommodityNum = (TextView) itemView.findViewById(R.id.tv_commodity_num);
            tvCommodityShowNum = (TextView) itemView.findViewById(R.id.tv_commodity_show_num);
            rlEdit = (LinearLayout) itemView.findViewById(R.id.rl_edit);
        }
    }

    public interface CheckInterface {

        void checkGroup(int position, boolean isChecked);
    }

}
